package com.example.vv.fragmentWork.utlis;

import android.app.Activity;
import android.content.SharedPreferences;

import com.example.vv.fragmentWork.entity.LoginEntity;

public class ShareUtils {
    public static UserInfo userInfo;
    public static boolean isLogin;
    public static SharedPreferences init(Activity activity){
        SharedPreferences vv = activity.getSharedPreferences("VV",activity.MODE_PRIVATE);
        return vv;
    }
    public static UserInfo getUserInfo(Activity activity) {
        if (userInfo != null) {
            isLogin = true;
            return userInfo;
        }
        SharedPreferences sharedPreferences = init(activity);
        String phone = sharedPreferences.getString("phone", null);
        String password = sharedPreferences.getString("password", null);
        String name = sharedPreferences.getString("name", null);
        String imgUrl = sharedPreferences.getString("imgUrl", null);
        if (phone==null) {
            isLogin=false;
            return null;
        }else {
            userInfo = new UserInfo();
            userInfo.setPhone(phone);
            userInfo.setPassword(password);
            userInfo.setName(name);
            userInfo.setImgUrl(imgUrl);
            isLogin = true;
            return userInfo;
        }
    }
    public static void addUserInfo(Activity activity, LoginEntity loginEntity){
        init(activity)
                .edit()
                .putString("phone",loginEntity.getId()+"")//id
                .putString("password",loginEntity.getPassword())
                .putString("name",loginEntity.getUsername())
                .putString("imgUrl",loginEntity.getIcon())
                .commit();
        isLogin = true;
    }
    public static void clearUserInfo(Activity activity){
        init(activity)
                .edit()
                .clear()
                .commit();
        isLogin = false;
    }
}
